﻿using Common;
using IBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Unmanned_MSUI.Areas.Admin.Controllers
{
    [Area("Admin")]

    public class AccountController : Controller
    {
        #region 构造函数
        //引入BLL
        private IStaffInfoBLL _staffInfoBLL;
        public AccountController(
            IStaffInfoBLL staffInfoBLL
            )
        {
            _staffInfoBLL = staffInfoBLL;
        }

        #endregion

        #region 显示视图的方法

        // GET: Account
        public IActionResult LoginView()
        {
            return View();
        }

        #endregion

        #region 登录的接口

        /// <summary>
        /// 登录的接口
        /// </summary>
        /// <param name="account">账号</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Login(string account, string password)
        {
            //实例化返回结果的对象 
            ReturnResult result = new ReturnResult();

            //判断账号是否为null或者""
            if (string.IsNullOrWhiteSpace(account))
            {
                result.Msg = "账号不能为空";
                return Json(result);
            }
            if (string.IsNullOrWhiteSpace(password))
            {
                result.Msg = "密码不能为空";
                return Json(result);
            }

            string msg;
            string staffName;
            string staffId;
            //调用登录业务逻辑
            bool isSuccess = _staffInfoBLL.Login(account, password, out msg, out staffName, out staffId);
            //把提示消息赋值给结果对象Msg属性
            result.Msg = msg;
            if (isSuccess)
            {
                result.IsSuccess = isSuccess;
                result.Code = 200;
                //result.Data = DateTime.Now.ToString();
                result.Data = staffName;
                //result.Datas = DateTime.Now;

                //Session
                HttpContext.Session.SetString("StaffId", staffId);
                HttpContext.Session.SetString("StaffName", staffName);
                //HttpContext.Session.GetString("UserId");//取

                //Cookie
                CookieOptions options = new CookieOptions()
                {
                    Expires = DateTime.Now.AddDays(100)
                };
                Response.Cookies.Append("StaffId", staffId,options);
                Response.Cookies.Append("StaffName", staffName,options);

                return Json(result);
            }
            else
            {
                result.Code = 500;
                return Json(result);
            }
        }

        #endregion
    }
}
